package au.edu.jcu.it.numberguess;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SettingActivity extends Activity {
	private SeekBar rangeSeekBar;
	private SeekBar timeClockSeekBar;
	private RadioGroup gmRadioGroup;
	private RadioGroup cmRadioGroup;
	private RadioGroup smRadioGroup;
	private NumberGuessApp app;
	private TextView tvBeatClock, tvRange;
	private ToggleButton toggleButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		app = (NumberGuessApp) getApplication();
		rangeSeekBar = (SeekBar) findViewById(R.id.seekBar1);
		gmRadioGroup = (RadioGroup) findViewById(R.id.rgGameMode);
		cmRadioGroup = (RadioGroup) findViewById(R.id.rgCardMode);
		smRadioGroup = (RadioGroup) findViewById(R.id.rgSocreMode);
		timeClockSeekBar = (SeekBar) findViewById(R.id.seekBar2);
		toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
		new CardSamples();
		new Bundle();
		tvBeatClock = (TextView) findViewById(R.id.tvBeatClock);
		tvRange = (TextView) findViewById(R.id.tvRange);
		// enable home button
		ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onStart() {

		rangeSeekBar.setProgress((app.gameSettings.getRangeNumber() - 20) / 10);
		rangeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				int size = progress * 10 + 20;

				tvRange.setText("Range 1-" + size);

			}
		});
		timeClockSeekBar.setProgress(app.gameSettings.getTimeClock() / 10);
		timeClockSeekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

					public void onStopTrackingTouch(SeekBar seekBar) {

					}

					public void onStartTrackingTouch(SeekBar seekBar) {

					}

					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						int size = progress * 10;
						tvBeatClock.setText("Time:" + size + "s");

					}
				});
		toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (!isChecked) {
					if (app.mediaPlayer.isPlaying()) {
						app.setlPause(app.mediaPlayer.getCurrentPosition());
						app.mediaPlayer.pause();

					}

				} else {
					System.out.println("playing");
					if (!app.mediaPlayer.isPlaying()) {
						app.mediaPlayer.seekTo(app.getlPause());
						app.mediaPlayer.start();
					}
				}

			}
		});
		super.onStart();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onBackPressed() {

		// get cardmode radiogroup checked
		TextView tvCM = (TextView) findViewById(cmRadioGroup
				.getCheckedRadioButtonId());
		System.out.println("CM:" + tvCM.getText());
		app.gameSettings.setCardMode((String) tvCM.getText());

		// get gamemode radio group checked
		TextView tvGM = (TextView) findViewById(gmRadioGroup
				.getCheckedRadioButtonId());
		System.out.println("GM:" + tvGM.getText());
		app.gameSettings.setGameMode((String) tvGM.getText());

		// get scoremode radio group checked
		TextView tvSM = (TextView) findViewById(smRadioGroup
				.getCheckedRadioButtonId());
		System.out.println("SM:" + tvSM.getText());
		app.gameSettings.setScoreMode((String) tvSM.getText());

		// get sound button value
		TextView tvSound = (TextView) findViewById(R.id.toggleButton1);
		System.out.println("Sound" + tvSound.getText());
		if (toggleButton.isChecked())
			app.gameSettings.setOnSound(true);
		else
			app.gameSettings.setOnSound(false);

		// get seekbar value
		app.gameSettings.setRangeNumber(rangeSeekBar.getProgress() * 10 + 20);
		app.setListGuess();
		app.backFromSetting = true;
		app.gameSettings.setTimeClock(timeClockSeekBar.getProgress() * 10);

		finish();
		super.onBackPressed();
	}

	@Override
	protected void onResume() {

		setUpView();
		super.onResume();
	}

	public void setUpView() {
		rangeSeekBar.setProgress((app.gameSettings.getRangeNumber() - 20) / 10);
		tvRange.setText("Range 1-" + app.gameSettings.getRangeNumber());
		timeClockSeekBar.setProgress(app.gameSettings.getTimeClock() / 10);
		tvBeatClock.setText("Time:" + app.gameSettings.getTimeClock());
		if (app.gameSettings.isOnSound())
			toggleButton.setChecked(true);
		else
			toggleButton.setChecked(false);
		if (app.gameSettings.getCardMode().trim().equals("BINARY"))
			cmRadioGroup.check(R.id.radioCM0);
		else {
			if (app.gameSettings.getCardMode().trim().equals("PRIME"))
				cmRadioGroup.check(R.id.radioCM1);
			else
				cmRadioGroup.check(R.id.radioCM2);
		}
		if (app.gameSettings.getGameMode().trim().equals("APP GUESS"))
			gmRadioGroup.check(R.id.radioGM0);
		else {
			if (app.gameSettings.getGameMode().trim().equals("PLAYER GUESS"))
				gmRadioGroup.check(R.id.radioGM1);
			else
				gmRadioGroup.check(R.id.radioGM2);
		}
		if (app.gameSettings.getScoreMode().trim().equals("TIME TRIAL"))
			smRadioGroup.check(R.id.radioSM0);
		else
			smRadioGroup.check(R.id.radioSM1);
		RadioButton rd1 = (RadioButton) findViewById(R.id.radioSM0);
		RadioButton rd2 = (RadioButton) findViewById(R.id.radioSM1);
		if (rd1.isChecked()) {
			timeClockSeekBar.setVisibility(View.INVISIBLE);
			tvBeatClock.setVisibility(View.INVISIBLE);
		}
		if (rd2.isChecked()) {
			timeClockSeekBar.setVisibility(View.VISIBLE);
			tvBeatClock.setVisibility(View.VISIBLE);

		}
		rd1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					timeClockSeekBar.setVisibility(View.INVISIBLE);
					tvBeatClock.setVisibility(View.INVISIBLE);
				}

			}
		});
		rd2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					timeClockSeekBar.setVisibility(View.VISIBLE);
					tvBeatClock.setVisibility(View.VISIBLE);
				}

			}
		});

	}
}
